using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSEngine.Stage;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;

namespace DSEngine.Sound
{
    public class SoundManager
    {
        private List<Sound> sound = new List<Sound>();
        public Screen Parent { get; set; }
        public SoundManager(Screen parentScreen)
        {
            this.Parent = parentScreen;
        }
        public Sound Load(string assitName)
        {
            SoundEffect s = Parent.Content.Load<SoundEffect>(assitName);
            Sound sound = new Sound(s.CreateInstance(), s);
            sound.Parent = this;
            return sound;
        }
        public Sound Play(string assitName,bool isLoop)
        {
            Sound s = Load(assitName);
            s.IsLoop = isLoop;
            s.Play();
            s.Parent = this;
            sound.Add(s);
            return s;
        }
        public void Remove(Sound s)
        {
            sound.Remove(s);
        }
        public void Update(GameTime gt)
        {
            List<Sound> s2remove = new List<Sound>();
            foreach (var item in sound)
            {
                item.Update(gt);
                if (item.IsLoop == false && item.GetState() == SoundState.Stopped)
                {
                    s2remove.Add(item);
                }
            }
            foreach (var item in s2remove)
            {
                Remove(item);
            }
        }
    }
}
